/*
 * @Author: dvlproad
 * @Date: 2023-12-11 09:48:39
 * @LastEditors: dvlproad
 * @LastEditTime: 2023-12-13 11:34:01
 * @Description: 
 */

import 'package:flutter/material.dart';

import './model/entity.dart';
import './widget/tool_item_widget.dart';
import './widget/main_card_widget.dart';
import './widget/pull_drag_widget.dart';
import './request/card_request_util.dart';

class CardHomePage2 extends StatefulWidget {
  const CardHomePage2({Key? key}) : super(key: key);

  @override
  _CardHomePage2State createState() => _CardHomePage2State();
}

class _CardHomePage2State extends State<CardHomePage2> {
  List<CardEntity>? _cardList;

  List<ToolBarEntity>? _toolbarList;

  @override
  initState() {
    super.initState();
    _requestCardsData();
  }

  void _requestCardsData() async {
    _cardList = await CardRequestUtil.loadCardList();
    _toolbarList = await CardRequestUtil.loadToolList();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: PullDragWidget(
        dragHeight: 100,
        parallaxRatio: 0.4,
        thresholdRatio: 0.3,
        header: _createHeader(),
        child: _createContent(),
      ),
    );
  }

  Widget _createHeader() {
    Widget header;
    if (_toolbarList == null || _toolbarList!.isEmpty) {
      header = const Text("Loading...");
    } else {
      header = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: _toolbarList!.map<Widget>(
          (item) {
            return ToolItemWidget(item: item);
          },
        ).toList(),
      );
    }

    return Container(
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: header,
    );
  }

  Widget _createContent() {
    if (_cardList == null || _cardList!.isEmpty) {
      return Container(
        alignment: Alignment.center,
        child: const Text(
          "Loading...",
        ),
      );
    }

    return Stack(
      children: <Widget>[
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            height: 100,
            padding: const EdgeInsets.only(left: 20, right: 20),
            // child: _createOptMenus(),
          ),
        ),
        CardStackWidget(
          cardList: _cardList!,
          offset: 8,
          cardCount: 2,
        )
      ],
    );
  }
}
